package com.thinkin.services.storage;

import java.sql.Clob;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import net.sf.json.JSONArray;

import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thinkin.allDo.SysDao;
import com.thinkin.services.util.InitDataHelp;
import com.thinkin.storage.jincang.po.WorderinSku;
import com.thinkin.storage.jincang.vo.WorderinVo;
import com.thinkin.util.ExtendDate;
import com.thinkin.util.JsonHelper;

/**
 * 进仓
 * 
 * @author Administrator
 * 
 */
public class Worderin {

	private Logger log = LoggerFactory.getLogger(this.getClass().getName());

	/**
	 * 查询
	 * 
	 * @param params
	 * @param clazz
	 * @return
	 */
	public String doQuery(String jsonstr) {
		String result = "{}";
		org.json.JSONObject jsonObject = JsonHelper.toJsonString(jsonstr);
		WorderinVo qvo = new WorderinVo();
		try {
			qvo.setOrderno(jsonObject.get("orderno").toString());
			qvo.setSkuname(jsonObject.get("skuname").toString());
			qvo.setOrderdatefrom(jsonObject.get("orderdatefrom").toString());
			qvo.setOrderdateto(jsonObject.get("orderdateto").toString());
			qvo.setOrgcd(jsonObject.get("orgcd").toString());
			qvo.setIorgcd(jsonObject.get("iorgcd").toString());
			qvo.setUploaddatefrom(jsonObject.get("uploaddatefrom").toString());
			qvo.setUploaddateto(jsonObject.get("uploaddateto").toString());
			qvo.setOrderstatus(jsonObject.get("orderstatus").toString());
			String clazz = jsonObject.get("clazz").toString();
			result = SysDao.getWorderinBuz().doQuery(qvo, clazz);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}

	public String doSaveAdd(String arrayparam, String skuItems) {
		org.json.JSONObject jsonObject = JsonHelper.toJsonString(arrayparam);
		org.json.JSONObject jsonSkuItems = JsonHelper.toJsonString(skuItems);
		JSONArray skuitems = JsonHelper.toJSONArray(jsonSkuItems);
		com.thinkin.storage.jincang.po.Worderin worderin = new com.thinkin.storage.jincang.po.Worderin();
		List<WorderinSku> skus = new ArrayList<WorderinSku>();
		try {
			Double quantityAll = 0d;
			Double pieceAll = 0d;
			Double grossweightAll = 0d;
			Double netweightAll = 0d;
			Double volumeAll = 0d;
			Double costAll = 0d;

			String pkeyid = UUID.randomUUID().toString();
			worderin.setKeyid(pkeyid);
			worderin.setOrderkeyid(jsonObject.get("orderkeyid").toString());
			worderin.setOrderinno(jsonObject.get("orderinno").toString());
			worderin.setOrderno(jsonObject.get("orderno").toString());
			worderin.setOrderdate(ExtendDate.stringToDate(jsonObject.get(
					"orderdate").toString()));
			worderin.setIorgcd(jsonObject.get("iorgcd").toString());
			worderin.setIorgname(jsonObject.get("iorgname").toString());
			worderin.setOrgcd(jsonObject.get("orgcd").toString());
			worderin.setOrgname(jsonObject.get("orgname").toString());
			worderin.setUploaddate(ExtendDate.stringToDate(jsonObject.get(
					"uploaddate").toString()));
			worderin.setActualdate(ExtendDate.stringToDate(jsonObject.get(
					"actualdate").toString()));
			worderin.setOrigincusid(jsonObject.get("origincusid").toString());
			worderin.setOrigincusname(jsonObject.get("origincusname")
					.toString());
			worderin.setSkuname(jsonObject.get("skuname").toString());
			char lottracked = (Character) jsonObject.get("lottracked");
			String cusid = jsonObject.get("cusid").toString();
			String cusname = jsonObject.get("cusname").toString();
			worderin.setLottracked(lottracked);
			worderin.setDictatetype(jsonObject.get("dictatetype").toString());
			worderin.setRemark(jsonObject.get("remark").toString());
			worderin.setOrderstatus(jsonObject.get("orderstatus").toString());
			worderin.setCusid(cusid);
			worderin.setCusname(cusname);
			String shippercusid = jsonObject.get("shippercusid").toString();
			String shippercusname = jsonObject.get("shippercusname").toString();
			worderin.setShippercusid(shippercusid);
			worderin.setShippercusname(shippercusname);
			worderin.setCreatename(jsonObject.get("createname").toString());
			worderin.setCreatedate(ExtendDate.stringToDate(jsonObject.get(
					"createdate").toString()));
			worderin.setModifyname(jsonObject.get("modifyname").toString());
			worderin.setModifydate(ExtendDate.stringToDate(jsonObject.get(
					"modifydate").toString()));
			worderin.setCheckname(jsonObject.get("checkname").toString());
			worderin.setCheckdate(ExtendDate.stringToDate(jsonObject.get(
					"checkdate").toString()));

			if (skuitems != null && skuitems.size() > 0) {
				for (int arry = 0; arry < skuitems.size(); arry++) {

					org.json.JSONObject obj = (org.json.JSONObject) skuitems
							.get(arry);
					WorderinSku skuitem = new WorderinSku();
					String ckeyid = UUID.randomUUID().toString();
					skuitem.setKeyid(ckeyid);
					skuitem.setOrderinkeyid(pkeyid);
					skuitem.setSno(Integer.valueOf(obj.get("sno").toString()));
					skuitem.setSkukeyid(obj.get("skukeyid").toString());
					skuitem.setSkuname(obj.get("skuname").toString());
					skuitem.setLotcode(obj.get("lotcode").toString());
					skuitem.setQuanlityname(obj.get("quanlityname").toString());
					skuitem.setSkucode(obj.get("skucode").toString());
					skuitem.setSkumodel(obj.get("skumodel").toString());
					skuitem.setSkuspec(obj.get("skuspec").toString());
					skuitem.setSkucolor(obj.get("skucolor").toString());
					skuitem.setSkubrand(obj.get("skubrand").toString());
					skuitem.setSkubarcode(obj.get("skubarcode").toString());
					skuitem.setPicpath(obj.get("picpath").toString());
					skuitem.setQuantityunit(obj.get("quantityunit").toString());
					skuitem.setPieceunit(obj.get("pieceunit").toString());
					skuitem.setVolumeunit(obj.get("volumeunit").toString());
					skuitem.setWeightunit(obj.get("weightunit").toString());

					double pieceskuitem = Double.parseDouble(obj.get("piece")
							.toString());
					double priceskuitem = Double.parseDouble(obj.get("price")
							.toString());

					double quantityskuitem = Double.parseDouble(obj.get(
							"quantity").toString());
					double costskuitem = Double.parseDouble(obj.get("skucost")
							.toString());
					double grossweightskuitem = Double.parseDouble(obj.get(
							"grossweight").toString());
					double netweightitem = Double.parseDouble(obj.get(
							"netweight").toString());
					double volumeitem = Double.parseDouble(obj.get("volume")
							.toString());

					costAll += costskuitem;
					quantityAll += quantityskuitem;
					pieceAll += pieceskuitem;
					grossweightAll += grossweightskuitem;
					netweightAll += netweightitem;
					volumeAll += volumeitem;
					skuitem.setQuantity(quantityskuitem);
					skuitem.setPrice(priceskuitem);
					skuitem.setPiece(pieceskuitem);

					skuitem.setGrossweight(grossweightskuitem);
					skuitem.setNetweight(netweightitem);
					skuitem.setVolume(volumeitem);
					skuitem.setQuantityact(Double.parseDouble(obj.get(
							"quantityact").toString()));
					skuitem.setPieceact(Double.parseDouble(obj.get("pieceact")
							.toString()));
					skuitem.setGrossweightact(Double.parseDouble(obj.get(
							"grossweightact").toString()));
					skuitem.setNetweightact(Double.parseDouble(obj.get(
							"netweightact").toString()));
					skuitem.setVolumeact(Double.parseDouble(obj
							.get("volumeact").toString()));
					skuitem.setSizeunit(obj.get("sizeunit").toString());
					skuitem.setLength(obj.get("length").toString());
					skuitem.setWidth(obj.get("width").toString());
					skuitem.setHigh(obj.get("high").toString());
					skuitem.setManufacturedate(ExtendDate.stringToDate(obj.get(
							"manufacturedate").toString()));

					skuitem.setCurrcd(obj.get("currcd").toString());
					skuitem.setSkucost(costskuitem);
					skuitem.setCostunit(obj.get("costunit").toString());
					skuitem.setConditioncode(obj.get("conditioncode")
							.toString());
					skuitem.setConditiondesc(obj.get("conditiondesc")
							.toString());
					skuitem.setRemark((Clob) obj.get("remark"));

					skuitem.setLottracked(lottracked);
					skuitem.setCusid(cusid);
					skuitem.setCusname(cusname);
					skuitem.setShippercusid(shippercusid);
					skuitem.setShippercusname(shippercusname);
					skus.add(skuitem);
				}
			}

			worderin.setQuantity(quantityAll);
			worderin.setPiece(pieceAll);
			worderin.setGrossweight(grossweightAll);
			worderin.setNetweight(netweightAll);
			worderin.setVolume(volumeAll);
			worderin.setSkucost(costAll);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	/*
	 * public String doSaveEdit(String arrayparam, String skuItems) {
	 * 
	 * }
	 */

	/**
	 * 查询条件初始化
	 * 
	 * @param clazz
	 * @return
	 */
	public String initFindModel(String clazz) {
		String result = SysDao.getWorderinBuz().initZorg(clazz);
		return result;
	}

	/**
	 * 新增/修改初始化
	 */
	public String initSaveModel(String zorgclazz, String cusclazz) {
		String initzorg = SysDao.getWorderinBuz().initZorg(zorgclazz);
		String initcus = SysDao.getWorderinBuz().initCus(cusclazz);
		String result = "{\"initzorg\":\"" + initzorg + ",\"initcus\":\""
				+ initcus + "\"}";
		return result;
	}

	/**
	 * 初始化商品明细
	 * 
	 */
	public String initWorderinSkuModel(String strJson) {
		String result = new InitDataHelp().GetInitData(strJson);
		return result;
	}
}
